package com.shangxian.giveithome.controller;


import com.shangxian.giveithome.annoaction.CheckLoginStatus;
import com.shangxian.giveithome.code.CommonCode;
import com.shangxian.giveithome.code.MechanismCode;
import com.shangxian.giveithome.entity.*;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.result.Result;
import com.shangxian.giveithome.service.MechanismService;
import com.shangxian.giveithome.service.VolunteerService;
import com.shangxian.giveithome.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api(tags = "机构模块")
@RestController
@RequestMapping("/mechanism")
public class MechanismController {

  @Autowired
  private MechanismService mechanismService;

  @Resource
  private PetFeiServer petFeiServer;

  @Autowired
  private RedisUtils redisUtils;

  @Autowired
  private HttpServletRequest request;

  @Autowired
  private VolunteerService volunteerService;

  @Resource
  private MechanismServer mechanismServer;

  @ApiOperation(value = "根据筛选条件查询机构列表")
  @GetMapping(value = "/queryByScreen", name = "根据筛选条件查询机构列表")
  public Result queryByScreen(MechanismScreen mechanismScreen, @PageableDefault(page = 0, size = 10) Pageable pageable) {

    PageableDetails pageableDetails = mechanismService.queryByScreen(mechanismScreen, pageable);

    return Result.success(pageableDetails);

  }

  @ApiOperation(value = "查询机构数据")
  @GetMapping("/queryMechanismData")
  public Result queryMechanism() {
    Long along = redisUtils.getMechanismCheck(request);
    if (along == -1L) {
      ExceptionUtils.error(MechanismCode.USER_NOT_S_AUTH);
    }
    return Result.success(mechanismService.queryByData(along));
  }

  @ApiOperation(value = "根据id查询机构详情")
  @GetMapping(value = "/queryById", name = "根据id查询机构详情")
  public Result queryById(Long id) {
    MechanismById mechanismById = mechanismService.queryById(id);
    if (mechanismById == null) {
      return Result.error(CommonCode.DATA_ERROR);
    }
    return Result.success(mechanismById);
  }

  @ApiOperation(value = "根据条件查询义工列表")
  @PostMapping(value = "/queryVolunteer")
  public Result queryVolunteerListAndToExamine(@RequestParam("sps") List<String> sps, @PageableDefault(page = 0, size = 10) Pageable pageable) {
    Long along = redisUtils.getMechanismCheck(request);
    if (along == -1L) {
      ExceptionUtils.error(MechanismCode.USER_NOT_S_AUTH);
    }
    return Result.success(volunteerService.queryVolunteerListAndToExamine(sps, along, pageable));


  }

  @ApiOperation(value = "根据查询义工审核列表")
  @PostMapping(value = "/queryVolunteerToExamine")
  public Result queryVolunteerListAndToExamine(@PageableDefault(page = 0, size = 10) Pageable pageable) {
    Long along = redisUtils.getMechanismCheck(request);
    if (along == -1L) {
      ExceptionUtils.error(MechanismCode.USER_NOT_S_AUTH);
    }
    return Result.success(volunteerService.queryVolunteerListToExamine(along, pageable));
  }

  @ApiOperation(value = "查看义工详情")
  @GetMapping(value = "/queryVolunteerDetails/{id}")
  public Result queryVolunteerDetails(@PathVariable Long id) {
    Long aLong = redisUtils.getMechanismCheck(request);
    if (aLong == -1L) {
      ExceptionUtils.error(MechanismCode.USER_NOT_S_AUTH);
    }
    return Result.success(volunteerService.queryVolunteerDetails(aLong, id));
  }

  @ApiOperation(value = "查看义工审核详情")
  @GetMapping(value = "/queryVolunteerExamineDetails/{id}")
  public Result queryVolunteerExamineDetails(@PathVariable Long id) {
    Long aLong = redisUtils.getMechanismCheck(request);
    if (aLong == -1L) {
      ExceptionUtils.error(MechanismCode.USER_NOT_S_AUTH);
    }
    return Result.success(volunteerService.queryVolunteerExamineDetails(aLong, id));
  }

  @ApiOperation(value = "根据机构id查询机构收容信息")
  @GetMapping(value = "/queryByAsylumOnId")
  public Result queryAsylumOnId(AsylumScreen asylumScreen, @PageableDefault(page = 0, size = 10) Pageable pageable) {
    Long along = redisUtils.getMechanismCheck(request);
    if (along == -1L) {
      ExceptionUtils.error(MechanismCode.USER_NOT_S_AUTH);
    }
    asylumScreen.setId(along);
    PageableDetails pageableDetails = petFeiServer.queryByAsylumById(asylumScreen, pageable.getPageNumber(), pageable.getPageSize());
    if (pageableDetails == null) {
      ExceptionUtils.error(CommonCode.DATA_ERROR);
    }
    return Result.success(pageableDetails);
  }

  @ApiOperation(value = "查看义工申请时机构列表")
  @GetMapping("/queryMechanismVolunteer")
  @CheckLoginStatus
  public Result queryMechanismVolunteer(@PageableDefault(page = 0, size = 10) Pageable pageable) {
    return Result.success(mechanismService.queryAllMechanism(pageable));
  }

  @ApiOperation(value = "申请创建协会")
  @PostMapping("/createMechanism")
  public Result create(@RequestBody @Validated MechanismSing mechanismSing) {
    UserDetails userDetails = redisUtils.getHeader(request);
    mechanismSing.setUserId(userDetails.getId());
    mechanismService.creatMechanism(mechanismSing);
    return Result.success();
  }

  @ApiOperation(value = "修改协会资料")
  @PutMapping("/updateMechanism")
  public Result update(@RequestBody @Validated MechanismSing mechanismSing) {
    Long along = redisUtils.getMechanismCheck(request);
    if (along == -1L) {
      ExceptionUtils.error(MechanismCode.USER_NOT_U_AUTH);
    }
    mechanismSing.setId(along);
    mechanismService.updateMechanism(mechanismSing);
    return Result.success(CommonCode.UPDATE_SUCCESS);
  }

  @ApiOperation(value = "查看机构义工邀请二维码")
  @GetMapping("/queryMeRqCode")
  public Result queryRqCode() {
    Long aLong = redisUtils.getMechanismCheck(request);
    if (aLong == -1L) {
      ExceptionUtils.error(MechanismCode.USER_NOT_S_AUTH);
    }
    return Result.success(mechanismService.queryMechanism(aLong));
  }

  @ApiOperation(value = "申请义工")
  @PostMapping("/createVolunteer")
  public Result createVolunteer(@Validated @RequestBody VolunteerCreate volunteerCreate) {
    UserDetails header = redisUtils.getHeader(request);
    volunteerCreate.setUs_id(header.getId());
    mechanismService.createVolunteer(volunteerCreate);
    return Result.success();
  }

  @ApiOperation(value = "通过")
  @PutMapping("/updateVolunteerSuccess")
  public Result updateSuccessVolunteer(Long id) {
      UserDetails header = redisUtils.getHeader(request);
        Long aLong = redisUtils.getMechanismCheck(header);
        if (aLong == -1L) {
            ExceptionUtils.error(MechanismCode.USER_NOT_U_AUTH);
        }
        volunteerService.successVolunteer(id, aLong);
        redisUtils.updateMechanism(request, mechanismServer.queryByMe(header.getId()));
        return Result.success();
  }

  @ApiOperation(value = "拒绝")
  @PutMapping("/updateVolunteerError")
  public Result updateErrorVolunteer(Long id) {
    Long aLong = redisUtils.getMechanismCheck(request);
    if (aLong == -1L) {
      ExceptionUtils.error(MechanismCode.USER_NOT_U_AUTH);
    }
    volunteerService.ErrorVolunteer(id, aLong);
    return Result.success();
  }

  @ApiOperation(value = "删除义工")
  @DeleteMapping("/deleteVolunteer/{id}")
  public Result deleteVolunteer(@PathVariable Long id) {
    Long aLong = redisUtils.getMechanismCheck(request);
    if (aLong == -1L) {
      ExceptionUtils.error(MechanismCode.USER_NOT_U_AUTH);
    }
    mechanismService.deleteVolun(aLong, id);
    return Result.success();
  }

}